--
-- Author: shunguo.chen
-- Date: 2017-01-10 11:30:57
--
-- HeadMenuController


local HeadMenuView = import("..views.HeadMenuView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(params)
	self.super.ctor(self, params)

end

function ClassRef:init()
	self._model = app:getInst("PlayerModel")

end

function ClassRef:removeListeners()
	self.super.removeListeners(self)
end

function ClassRef:listEvent()
    return {

	}
end


function ClassRef:onEvent(eventName, data)
	if eventName == "" then

	end
end

-- data = {
--	 type = Enums.MenuType.,
--	 pos = {},
--	 attr = UserBaseInfo,  --type=player/friend使用这个参数
--	 online = true/false,  --是否在线，优先级比attr里面的online高，处理attr是proto时
-- }
function ClassRef:showView(data)
	data.type = data.type or Enums.MenuType.player

	-- 跨服副本屏蔽角色菜单
	if DungeonManager:isCrossDungeon() and 
		(data.type == Enums.MenuType.player or 
		data.type == Enums.MenuType.teamRobot or
		data.type == Enums.MenuType.teamPartner) then
		return
	end

	-- dump(data, "HeadMenuController.showView data")
	data.funcs = self._model:getMenuList(data)
	if 0 >= #data.funcs then return end

	if cc.isDead(self._view) then
		self._view = HeadMenuView.new(app.mvc.VIEWTYPE_NORMAL)
		self._view:onHideView(handler(self, self.hideView))
		self._view:onOperate(handler(self, self.onOperate))
		self:addChild(self._view)
	end

	-- print("HeadMenuView showView")
	self._view:show()

	local pos = data.pos or cc.p(display.width / 2,display.height / 2)
	self._view:refresh(data)
end
function ClassRef:hideView(data)
	-- print("HeadMenuView hideView")
	if not self:isViewAlive() then return end
	self._view:hide()
end

function ClassRef:isHeadMenuVisible()
	local isVisible = self:isViewVisible()
	if isVisible then
		if self._view:getType() == Enums.MenuType.head then
			return true
		end
	end
	return false
end

---------------------------- view logic ---------------------------
function ClassRef:onOperate(op, data)
	-- data 和打开headMenu的一样

	local attr = data.attr
	local online = data.online
	local userInfo = data.userInfo
	local guildInfo = data.guildInfo
	-- 判断是否在线，不在线给出提示
	if online ~= nil then
		online = online
	elseif attr then
		if attr.online ~= nil then
			online = attr.online
		else
			online = true
		end
	end

	if op == Enums.MenuItems.observe then
		if userInfo then
			app:sendMsg("OtherPlayerController", "openView", userInfo)
		else
			EM:notify("onWatchUI", attr.uid)
		end
	    self:hideView()
	elseif op ==  Enums.MenuItems.friend then
		app:sendMsg("FriendModel", "reqAddFriendByUID", attr.uid)
		self:hideView()
	elseif op == Enums.MenuItems.delFriend then
		app:sendMsg("FriendModel", "reqRemoveFriendByUID", attr.uid)
		self:hideView()
	elseif op ==  Enums.MenuItems.black then
		app:sendMsg("FriendModel", "reqAddBlacklist", attr.uid)
		self._view:refreshBtns(self._model:getPlayerMenuList(attr.uid))
		self:hideView()
	elseif op ==  Enums.MenuItems.mail then
		app:sendMsg("MailController", "writeMail", attr.uid)
		self:hideView()
	elseif op ==  Enums.MenuItems.chat then
		--密聊
		if not online then
			display.pushToast(L("tips_1050011"))
		else
			if attr.isRobot and attr:isRobot() then
			    display.pushToast(L("lua_code_text_380"))
			else
			    app:sendMsg("IMRoomController", "speakToTarget", attr)
			end
		end
	    self:hideView()
	elseif op ==  Enums.MenuItems.achive then
		-- 成就界面
		UIHandler:handleMessage({id = Constant.FUNC_TYPE_CJ,mode = "open",labelType = 1})
		self:hideView()
	elseif op ==  Enums.MenuItems.friendView then
		-- 好友界面
		UIHandler:handleMessage({id = Constant.FUNC_TYPE_FRIEND,mode = "open"})
		self:hideView()
	elseif op ==  Enums.MenuItems.captainRoom then
		-- 船长室
		app:sendMsg("CaptainController", "openCaptainView", UD.uid)
		self:hideView()
	elseif op ==  Enums.MenuItems.visitCaptainRoom then
		app:sendMsg("CaptainController", "openCaptainView", attr.uid)
		self:hideView()
	elseif op ==  Enums.MenuItems.set then
		-- 设置
		 UIHandler:handleMessage({id = Constant.FUNC_TYPE_SET,mode = "open"})
	    self:hideView()
	elseif op ==  Enums.MenuItems.switchUser then
		-- 切换角色
		local conformDialog = display.newConfirmDialog( L("lua_code_text_445"),
            function() -- 确定
                app:relaunchApp(1)
            end)
        UIHandler:addPopDialog(conformDialog)
	    self:hideView()
	elseif op == Enums.MenuItems.teamInvite then
		-- 要求入队
		if not online then
			display.pushToast(L("tips_1050011"))
		else
		    app:sendMsg("TeamModel", "reqInviteToTeam", attr.uid, attr.level)
		end
        self:hideView()
	elseif op == Enums.MenuItems.teamJoin then
		-- 加入队伍
		if not online then
			display.pushToast(L("tips_1050011"))
		else
		    app:sendMsg("TeamModel", "reqApplyJoinTeam", attr.uid)
		end
        self:hideView()
	elseif op ==  Enums.MenuItems.teamLeader then
		-- 转让队长
		-- 副本里不能转让队长 暂时去掉
		-- if DungeonManager:isInDungeon() then
		-- 	display.pushToast( L("副本里不能转让队长") )
		-- else
		-- 	app:sendMsg("TeamModel", "reqPromoteTeamLeader", attr.uid)
		-- end
		app:sendMsg("TeamModel", "reqPromoteTeamLeader", attr.uid)
		self:hideView()
	elseif op == Enums.MenuItems.teamImpeach then
		-- 申请变更队长
        app:sendMsg("TeamModel", "reqImpeachLeader")
		self:hideView()
	elseif op ==  Enums.MenuItems.teamKick then
		-- 踢出队伍
		app:sendMsg("TeamModel", "reqKickTeamMember", attr.uid)
		self:hideView()
	elseif op ==  Enums.MenuItems.teamQuit then
		-- 退出队伍
		app:sendMsg("TeamModel", "reqQuitTeam")
		self:hideView()
	elseif op ==  Enums.MenuItems.teamSwitch then
		-- 转化队伍／战团

		self:hideView()
	elseif op ==  Enums.MenuItems.teamReady then
		-- 战前准备

		self:hideView()
	elseif op ==  Enums.MenuItems.teamBack then
		-- 回归队伍
		app:sendMsg("TeamModel", "reqFollowLeader", true)
		self:hideView()
	elseif op ==  Enums.MenuItems.teamLeave then
		-- 暂离队伍
		app:sendMsg("TeamModel", "reqFollowLeader", false)
		self:hideView()
	elseif op ==  Enums.MenuItems.kickRobot then
		-- 踢掉机器人
		app:sendMsg("TeamModel", "reqKickTeamMember", attr.uid, true)
		self:hideView()
	elseif op ==  Enums.MenuItems.kickPartner then
		if data.uid then
			UD.partnersData:C2S_EmployPartner(data.uid)
		end
		self:hideView()
	elseif op ==  Enums.MenuItems.changePartner then
		app:sendMsg("NewTeamApplyController", "openView", { 1, 5 } ) -- 弹出邀请-随从标签
		self:hideView()
	elseif op ==  Enums.MenuItems.guildOfficer then --任命精英
		app:sendMsg("NewguildModel", "reqAppointment", attr:getMemberId(), 2 )
		self:hideView()
	elseif op ==  Enums.MenuItems.guildUnOfficer then --取消精英
		app:sendMsg("NewguildModel", "reqAppointment", attr:getMemberId(), 3 )
		self:hideView()
	elseif op ==  Enums.MenuItems.guildLeader then --转让会长
		local conformDialog = display.newConfirmDialog( L("lua_code_text_272"),
			function() -- 确定
				app:sendMsg("NewguildModel", "reqAppointment", attr:getMemberId(), 1 )
			end)
		UIHandler:addPopDialog(conformDialog)
		self:hideView()
	elseif op ==  Enums.MenuItems.guildDisband then --解散公会
		local confirmDialog = display.newConfirmDialog( L("guild_disband_warning"),
			function() -- 确定
				app:sendMsg("NewguildModel", "reqDismissGuild")
			end)
		UIHandler:addPopDialog( confirmDialog )
		self:hideView()
	elseif op ==  Enums.MenuItems.guildKick then --踢出公会
		local conformDialog = display.newConfirmDialog( L("lua_code_text_275"),
			function() -- 确定
				app:sendMsg("NewguildModel", "reqKickMember", attr:getMemberId())
			end)
		UIHandler:addPopDialog(conformDialog)
		self:hideView()
	elseif op ==  Enums.MenuItems.guildQuit then --退出公会
		local confirmDialog = display.newConfirmDialog(L("tips_1130012"),
			function() -- 确定
				app:sendMsg("NewguildModel", "reqQuitGuild")
			end)
		UIHandler:addPopDialog( confirmDialog )
		self:hideView()
	elseif op ==  Enums.MenuItems.guildInvite then --邀请入会
		app:sendMsg("NewguildModel", "reqJoinGuild", attr.uid)
		self:hideView()
	elseif op == Enums.MenuItems.guildPeerageChange then
		app:sendMsg("NewguildController", "reqChangePeerage", attr.uid)
		self:hideView()
	elseif op == Enums.MenuItems.duelKickPartner then
		--dump("踢出随从")
		--dump(data)
		app:sendMsg("PvpController", "removeDuelPartner", data.partnerId)
		self:hideView()
	elseif op == Enums.MenuItems.duelChoosePartner then
		--dump("选择随从")
		--dump(data)
		app:sendMsg("PvpController", "openPvpDuelTeamView")
		self:hideView()
	elseif op == Enums.MenuItems.report then --举报
		local info = userInfo ~= nil and userInfo or attr
		app:sendMsg("ReportController", "openView", info)
		self:hideView()
	elseif op == Enums.MenuItems.eliteDuelKickPartner then
		UD.csEliteMatch:C2S_OnBattlePartnerChange(data.partnerId, false)
		self:hideView()
	elseif op == Enums.MenuItems.eliteDuelChoosePartner then
		app:sendMsg("PvpEliteController", "openEliteMatchDuelTeamView")
		self:hideView()
	elseif op == Enums.MenuItems.guildInfo then
		if guildInfo then
			app:sendMsg("SuperRankController", "popGuildDetailView", guildInfo)
			self:hideView()
		end
	end
end

return ClassRef
